package com.goose.bmm.controler.fragment;

import com.goose.bmm.R;
import com.goose.bmm.model.business.DataGlobal;

import android.R.bool;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class ScoreBoardFragment extends Fragment implements OnClickListener,
		OnLongClickListener {

	private View mView;
	private Button mButtonPlayPause, mButton1Pts, mButton2Pts, mButton3Pts,
			mButtonReset, mButtonFoul, mButtonTimeOut, mButtonSubsitution,
			mButtonTurnOver;

	private TextView mTextViewPeriod, mTextViewShotTime, mTextViewTimeLimit,
			mTextViewHomeScore, mTextViewAwayScore, mTextViewHomeTeamFoul,
			mTextViewAwayTeamFoul, mTextViewHomeTimeOut, mTextViewAwayTimeOut,
			mTextViewHomeFieldGoal, mTextViewAwayFieldGoal, mTextViewHome3Pts,
			mTextViewAway3Pts, mTextViewHomeFreeThrow, mTextViewAwayFreeThrow,
			mTextViewHomeDunk, mTextViewAwayDunk, mTextViewHomeAssist,
			mTextViewAwayAssist, mTextViewHomeSteal, mTextViewAwaySteal,
			mTextViewHomeBlock, mTextViewAwayBlock, mTextViewHomeOffRebound,
			mTextViewAwayOffRebound, mTextViewHomeDefRebound,
			mTextViewAwayDefRebound;

	private boolean pauseFlag;
	private String[] periodNumber;

	private int shotTimeCurrent;
	private int shotTimeCurrentSecond;
	private int timeLimitCurrent;
	private int periodCurrent;
	private int homeFoulCurrent;
	private int awayFoulCurrent;
	private boolean flagCreated;

	public ScoreBoardFragment() {
		mThread.start();
		flagCreated = false;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		mView = inflater.inflate(R.layout.fragment_board_score, container,
				false);
		periodNumber = getActivity().getResources().getStringArray(
				R.array.period_number);

		// initialize button
		mButtonPlayPause = (Button) mView.findViewById(R.id.btn_play_pause);
		mButton1Pts = (Button) mView.findViewById(R.id.btn_1_pts);
		mButton2Pts = (Button) mView.findViewById(R.id.btn_2_pts);
		mButton3Pts = (Button) mView.findViewById(R.id.btn_3_pts);
		mButtonReset = (Button) mView.findViewById(R.id.btn_reset);
		mButtonFoul = (Button) mView.findViewById(R.id.btn_foul);
		mButtonTimeOut = (Button) mView.findViewById(R.id.btn_time_out);
		mButtonSubsitution = (Button) mView.findViewById(R.id.btn_subsitution);
		mButtonTurnOver = (Button) mView.findViewById(R.id.btn_reset);

		mButton1Pts.setOnClickListener(this);
		mButton2Pts.setOnClickListener(this);
		mButton3Pts.setOnClickListener(this);
		mButtonFoul.setOnClickListener(this);
		mButtonPlayPause.setOnClickListener(this);
		mButtonReset.setOnClickListener(this);
		mButtonSubsitution.setOnClickListener(this);
		mButtonTimeOut.setOnClickListener(this);
		mButtonTurnOver.setOnClickListener(this);

		// initialize textview
		mTextViewPeriod = (TextView) mView.findViewById(R.id.tv_period);
		mTextViewShotTime = (TextView) mView.findViewById(R.id.tv_shot_time);
		mTextViewTimeLimit = (TextView) mView.findViewById(R.id.tv_time_limit);
		mTextViewHomeScore = (TextView) mView.findViewById(R.id.tv_home_score);
		mTextViewAwayScore = (TextView) mView.findViewById(R.id.tv_away_score);
		mTextViewHomeTeamFoul = (TextView) mView
				.findViewById(R.id.tv_home_team_fouls);
		mTextViewHomeTimeOut = (TextView) mView
				.findViewById(R.id.tv_home_time_out);
		mTextViewHomeFieldGoal = (TextView) mView
				.findViewById(R.id.tv_home_field_goals);
		mTextViewHome3Pts = (TextView) mView.findViewById(R.id.tv_home_3_pts);
		mTextViewHomeFreeThrow = (TextView) mView
				.findViewById(R.id.tv_home_free_throw);
		mTextViewHomeDunk = (TextView) mView.findViewById(R.id.tv_home_dunks);
		mTextViewHomeAssist = (TextView) mView
				.findViewById(R.id.tv_home_assists);
		mTextViewHomeSteal = (TextView) mView.findViewById(R.id.tv_home_steal);
		mTextViewHomeBlock = (TextView) mView.findViewById(R.id.tv_home_block);
		mTextViewHomeOffRebound = (TextView) mView
				.findViewById(R.id.tv_home_off_rebound);
		mTextViewHomeDefRebound = (TextView) mView
				.findViewById(R.id.tv_home_def_rebound);
		mTextViewAwayTeamFoul = (TextView) mView
				.findViewById(R.id.tv_away_team_fouls);
		mTextViewAwayTimeOut = (TextView) mView
				.findViewById(R.id.tv_away_time_out);
		mTextViewAwayFieldGoal = (TextView) mView
				.findViewById(R.id.tv_away_field_goals);
		mTextViewAway3Pts = (TextView) mView.findViewById(R.id.tv_away_3_pts);
		mTextViewAwayFreeThrow = (TextView) mView
				.findViewById(R.id.tv_away_free_throw);
		mTextViewAwayDunk = (TextView) mView.findViewById(R.id.tv_away_dunk);
		mTextViewAwayAssist = (TextView) mView
				.findViewById(R.id.tv_away_assist);
		mTextViewAwaySteal = (TextView) mView.findViewById(R.id.tv_away_steal);
		mTextViewAwayBlock = (TextView) mView.findViewById(R.id.tv_away_block);
		mTextViewAwayOffRebound = (TextView) mView
				.findViewById(R.id.tv_away_off_rebound);
		mTextViewAwayDefRebound = (TextView) mView
				.findViewById(R.id.tv_away_def_rebound);

		mTextViewPeriod.setOnLongClickListener(this);
		mTextViewShotTime.setOnLongClickListener(this);
		mTextViewTimeLimit.setOnClickListener(this);
		mTextViewHomeScore.setOnLongClickListener(this);
		mTextViewAwayScore.setOnLongClickListener(this);

		// initialize match
		if (flagCreated == false) {
			initMatch();
			flagCreated = true;
		} else {
			updateInfo();
		}

		return mView;
	}

	private void initMatch() {
		pauseFlag = true;
		homeFoulCurrent = 0;
		awayFoulCurrent = 0;
		shotTimeCurrent = DataGlobal.matchGlobal.getShotTime();
		timeLimitCurrent = DataGlobal.matchGlobal.getTimePeriod() * 60;

		mTextViewPeriod.setText(periodNumber[0]);
		mTextViewShotTime.setText(DataGlobal.matchGlobal.getShotTime() + "");
		mTextViewTimeLimit.setText(DataGlobal.matchGlobal.getTimePeriod()
				+ ":00");

		mTextViewHomeScore.setText(DataGlobal.homeTeamGlobal.getScore() + "");
		mTextViewHomeTeamFoul.setText("0 / 5");
		mTextViewHomeTimeOut.setText("0 / 7");
		mTextViewHomeFieldGoal.setText("0 / 0 (0%)");
		mTextViewHome3Pts.setText("0 / 0 (0%)");
		mTextViewHomeFreeThrow.setText("0 / 0 (0%)");
		mTextViewHomeDunk.setText("0");
		mTextViewHomeAssist.setText("0");
		mTextViewHomeSteal.setText("0");
		mTextViewHomeBlock.setText("0");
		mTextViewHomeOffRebound.setText("0");
		mTextViewHomeDefRebound.setText("0");

		mTextViewAwayScore.setText(DataGlobal.awayTeamGlobal.getScore() + "");
		mTextViewAwayTeamFoul.setText("0 / 5");
		mTextViewAwayTimeOut.setText("0 / 7");
		mTextViewAwayFieldGoal.setText("0 / 0 (0%)");
		mTextViewAway3Pts.setText("0 / 0 (0%)");
		mTextViewAwayFreeThrow.setText("0 / 0 (0%)");
		mTextViewAwayDunk.setText("0");
		mTextViewAwayAssist.setText("0");
		mTextViewAwaySteal.setText("0");
		mTextViewAwayBlock.setText("0");
		mTextViewAwayOffRebound.setText("0");
		mTextViewAwayDefRebound.setText("0");
	}

	public void updateInfo() {
		mTextViewPeriod.setText(periodNumber[periodCurrent]);
		if (shotTimeCurrent > 9) {
			mTextViewShotTime.setText(shotTimeCurrent + "");
		} else {
			mTextViewShotTime.setText(shotTimeCurrent + "."
					+ shotTimeCurrentSecond);
		}
		if (timeLimitCurrent % 60 < 10) {
			mTextViewTimeLimit.setText(timeLimitCurrent / 60 + ":0"
					+ timeLimitCurrent % 60);
		} else {
			mTextViewTimeLimit.setText(timeLimitCurrent / 60 + ":"
					+ timeLimitCurrent % 60);
		}
		mTextViewHomeScore.setText(DataGlobal.homeTeamGlobal.getScore() + "");
		mTextViewAwayScore.setText(DataGlobal.awayTeamGlobal.getScore() + "");
		mTextViewHomeTeamFoul.setText(homeFoulCurrent + "");
		mTextViewAwayTeamFoul.setText(awayFoulCurrent + "");
	}

	private Thread mThread = new Thread(new Runnable() {

		@Override
		public void run() {
			shotTimeCurrentSecond = 9;
			while (true) {
				while (!pauseFlag && shotTimeCurrent > 0) {
					if (shotTimeCurrentSecond == 0) {
						shotTimeCurrentSecond = 9;
					}
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}

					shotTimeCurrentSecond--;

					if (shotTimeCurrentSecond == 0) {
						shotTimeCurrent--;
						timeLimitCurrent--;
					}
					updateTime();
				}
			}
		}
	});

	private void updateTime() {
		mTextViewShotTime.post(new Runnable() {

			@Override
			public void run() {
				if (shotTimeCurrent < 10) {
					mTextViewShotTime.setText(shotTimeCurrent + "."
							+ shotTimeCurrentSecond);
				} else {
					mTextViewShotTime.setText(shotTimeCurrent + "");
				}
				if (timeLimitCurrent % 60 < 10) {
					mTextViewTimeLimit.setText(timeLimitCurrent / 60 + ":0"
							+ timeLimitCurrent % 60);
				} else {
					mTextViewTimeLimit.setText(timeLimitCurrent / 60 + ":"
							+ timeLimitCurrent % 60);
				}
				if (shotTimeCurrent == 0 && shotTimeCurrentSecond == 0) {
					pauseFlag = true;
					shotTimeCurrent = DataGlobal.matchGlobal.getShotTime();
				}

			}
		});
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.btn_play_pause: {
			if (pauseFlag) {
				pauseFlag = false;
			} else {
				pauseFlag = true;
			}
			break;
		}
		case R.id.btn_1_pts: {
			break;
		}
		case R.id.btn_2_pts: {
			break;
		}
		case R.id.btn_3_pts: {
			break;
		}
		case R.id.btn_reset: {
			break;
		}
		case R.id.btn_foul: {
			break;
		}
		case R.id.btn_time_out: {
			break;
		}
		case R.id.btn_subsitution: {
			break;
		}
		
		}
	}

	@Override
	public boolean onLongClick(View arg0) {
		return false;
	}
}
